package cn.itcast.web.controller.basics;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/basics/factory",name = "厂家管理")
public class FactoryController extends BaseController {


    //依赖注入
    @Reference
    private FactoryService factoryService;

    //分页查询
    @RequestMapping(value = "/list",name = "分页查询")
    public String select(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){

       //查询对象
       FactoryExample example = new FactoryExample();
       //排序
       example.setOrderByClause("create_time desc");
       //获取方法
       PageInfo page = factoryService.findByPage(pageNum, pageSize, example);
       //存入域中
       request.setAttribute("page",page);
        //跳转页面
        return "system/factory/factory-list";

    }

    //跳转新增页面
    @RequestMapping(value = "/toAdd",name = "跳转新增页面")
    public String toAdd(){
        return "system/factory/factory-add";
    }

    //新增
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(Factory factory){

        //判断
        if (StrUtil.hasEmpty(factory.getId())){
            //新增
            //id
            factory.setId(IdUtil.simpleUUID());
            //调用方法
            factoryService.save(factory);

        }else{
            //修改
            factoryService.update(factory);
        }

        //重定向
        return "redirect:/basics/factory/list.do";

    }

    //跳转修改页面
    @RequestMapping(value = "/toUpdate",name = "跳转修改页面")
    public String toUpdate(String id){

        //调用方法
        Factory page = factoryService.findById(id);

        //存入域中
        request.setAttribute("page",page);

        //跳转页面
        return "system/factory/factory-update";
    }

    //删除厂家
    @RequestMapping(value = "/delete",name = "删除厂家")
    public String delete(String id){
        //调用方法
        factoryService.delete(id);
        //从定向页面
        return "redirect:/basics/factory/list.do";
    }

}
